#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

#define FOR(i,a,n) for(int i=a;i<n;++i)
#define REP(i,n) FOR(i,0,n)
#define v2 first
#define id second

using namespace std;

typedef pair<int,int> pii;

int ans[10010];
vector<pii> query[40400];

class Graph
{
    struct Edge
    {
        int vs,vt,w;
        Edge* next;
        void set(int a,int b,int c,Edge* nt = NULL)
        {
            vs=a,vt=b,w=c,next=nt;
        }
    };
#define EACH(i,vs) for(Edge* i=head[vs];i;i=i->next)
  public:
  int vn,arcn;
  Edge *edge,**head;
  Graph(int n,int m)
  {
    vn=n+1,arcn=0;
    edge = new Edge[m+10];
    head = new Edge*[n+10];
    fill(head,head+n+10,(Edge*)NULL);
  }
  ~Graph()
  {
    delete(edge);
    delete(head);
  }
  void AddArc(int vs,int vt,int w)
  {
    edge[arcn].set(vs,vt,w,head[vs]);
    head[vs]=&edge[arcn++];
  }
  int father[40400];
  int dist[40400];
  int GetGroup(int v)
  {
    if(father[v]!=v) father[v]=GetGroup(father[v]);
    return father[v];
  }
  void dfs(int vs)
  {
    REP(i,query[vs].size())
    {
        int v2 = query[vs][i].v2;
        int id = query[vs][i].id;
        if(dist[vs]!=-1)
        {
            ans[id]=dist[vs]+dist[v2]-2*dist[GetGroup(v2)];
        }
    }
    EACH(ep,vs) if(dist[ep->vt]==-1)
    {
        dist[ep->vt] = dist[vs]+ep->w;
        dfs(ep->vt);
        father[ep->vt]=vs;
    }
  }
  void GetAns(int k)
  {
    REP(i,vn+1) dist[i]=-1,father[i]=i;
    dist[1]=0;
    dfs(1);
    REP(i,k) printf("%d\n",ans[i]);
  }
};
int main()
{
    int n,m;
    cin>>n>>m;
    Graph g(n,m*2);
    REP(i,m)
    {
        int v1,v2,w;
        char dir[2];
        scanf("%d%d%d%s",&v1,&v2,&w,dir);
        g.AddArc(v1,v2,w);
        g.AddArc(v2,v1,w);
    }
    int k;
    cin>>k;
    REP(i,k)
    {
        int v1,v2;
        scanf("%d%d",&v1,&v2);
        query[v1].push_back(pii(v2,i));
        query[v2].push_back(pii(v1,i));
    }
    g.GetAns(k);
	return 0;
}
